package com.qilu.pe.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.qilu.pe.MainActivity;
import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.WxUserInfo;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.ActiviceCollector;
import com.qilu.pe.support.util.Logg;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.util.ToastUtils;

import java.util.List;

import androidx.appcompat.widget.AppCompatEditText;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 输入密码
public class InputPwdActivity extends Base2Activity {
    private AppCompatEditText et_pwd;
    private TextView tv_ok;

    WxUserInfo wxUserInfo;// 需要绑定的微信用户对象

    String pwd;
    String phone, code;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_input_pwd;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiviceCollector.addActivity(this);
        et_pwd = findViewById(R.id.et_pwd);
        tv_ok = findViewById(R.id.tv_ok);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            wxUserInfo = (WxUserInfo) bundle.getSerializable("wxUserInfo");
            phone = bundle.getString("phone");
            code = bundle.getString("code");
        }

        setHeadTitle("输入密码");

        setOnClickListener(tv_ok);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_ok) {
            pwd = et_pwd.getText().toString().trim();
            if (TextUtils.isEmpty(pwd)) {
                ToastUtils.showShort("请输入密码");
                return;
            }
            reqWxLogin();
        }
    }

    // 微信登录
    private Disposable reqWxLogin() {
        return APIRetrofit.getDefault().wxLogin(wxUserInfo.getUnionid(), "1", wxUserInfo.getHeadimgurl(), phone, pwd, code)
                .compose(RxUtil.<BaseResult2>subIoObMain())
                .subscribe(new Consumer<BaseResult2>() {
                    @Override
                    public void accept(BaseResult2 baseResult2) throws Exception {
                        hideProgress();
                        if (baseResult2.isSuccess()) {
                            List<LinkedTreeMap<String, LinkedTreeMap<String, String>>> loginList = (List) baseResult2.getData();
                            if (loginList.size() >= 1) {
                                Logg.i("loginList.get(0) = " + loginList.get(0).getClass());
                                Logg.i("loginList.get(0) = " + loginList.get(0).get("original").getClass());

                                Global.HEADER = loginList.get(0).get("original").get("token_type") + " " + loginList.get(0).get("original").get("access_token");
                                startActivity(MainActivity.class);
                                finish();
                            }
                        } else {
                            if (TextUtils.equals("4001", baseResult2.getCode())) {// 注册成功,完善信息
                                LinkedTreeMap<String, Integer> user = (LinkedTreeMap) baseResult2.getData();
                                String tmp = user.get("id") + "";
                                if (tmp.contains(".")) {
                                    Global.USER_ID = tmp.substring(0, tmp.lastIndexOf("."));
                                } else {
                                    Global.USER_ID = tmp;
                                }
                                Logg.i("Global.USER_ID = " + Global.USER_ID);
                                startActivity(RealAuthAgentActivity.class);
                                finish();
                            } else if (TextUtils.equals("4002", baseResult2.getCode())) {// 微信绑定手机号
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("wxUserInfo", wxUserInfo);// 微信用户对象
                                startActivity(BindPhoneActivity.class, bundle);
                                finish();
                            }
                        }
                        ToastUtils.showShort(baseResult2.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("登录失败");
                    }
                });
    }
}